"""
用户管理模型
包含用户基本信息、角色权限等
"""

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone


class User(AbstractUser):
    """
    用户模型 - 继承Django内置的AbstractUser
    扩展了用户的基本信息
    """
    
    # 用户角色选择
    ROLE_CHOICES = [
        ('student', '学生'),
        ('teacher', '教师'),
        ('admin', '管理员'),
        ('errand_runner', '跑腿员'),
    ]
    
    # 性别选择
    GENDER_CHOICES = [
        ('male', '男'),
        ('female', '女'),
        ('other', '其他'),
    ]
    
    # 基础信息
    role = models.CharField(
        max_length=20, 
        choices=ROLE_CHOICES, 
        default='student',
        verbose_name='用户角色'
    )
    
    # 个人信息
    nickname = models.CharField(
        max_length=50, 
        blank=True, 
        verbose_name='昵称'
    )
    avatar = models.ImageField(
        upload_to='avatars/', 
        blank=True, 
        null=True,
        verbose_name='头像'
    )
    phone = models.CharField(
        max_length=11, 
        blank=True,
        verbose_name='手机号'
    )
    gender = models.CharField(
        max_length=10, 
        choices=GENDER_CHOICES, 
        blank=True,
        verbose_name='性别'
    )
    birthday = models.DateField(
        blank=True, 
        null=True,
        verbose_name='生日'
    )
    
    # 学校信息
    student_id = models.CharField(
        max_length=20, 
        blank=True,
        verbose_name='学号'
    )
    major = models.CharField(
        max_length=100, 
        blank=True,
        verbose_name='专业'
    )
    grade = models.CharField(
        max_length=20, 
        blank=True,
        verbose_name='年级'
    )
    dormitory = models.CharField(
        max_length=50, 
        blank=True,
        verbose_name='宿舍'
    )
    
    # 状态信息
    is_verified = models.BooleanField(
        default=False,
        verbose_name='是否实名认证'
    )
    is_active = models.BooleanField(
        default=True,
        verbose_name='是否激活'
    )
    
    # 时间信息
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )
    
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'
        db_table = 'users'
    
    def __str__(self):
        return f"{self.username} ({self.get_role_display()})"


class UserProfile(models.Model):
    """
    用户详细信息模型
    存储用户的扩展信息
    """
    
    user = models.OneToOneField(
        User, 
        on_delete=models.CASCADE,
        related_name='profile',
        verbose_name='用户'
    )
    
    # 个人简介
    bio = models.TextField(
        blank=True,
        verbose_name='个人简介'
    )
    
    # 兴趣爱好
    interests = models.JSONField(
        default=list,
        blank=True,
        verbose_name='兴趣爱好'
    )
    
    # 信用评分（用于跑腿服务）
    credit_score = models.IntegerField(
        default=100,
        verbose_name='信用评分'
    )
    
    # 钱包余额
    balance = models.DecimalField(
        max_digits=10, 
        decimal_places=2,
        default=0.00,
        verbose_name='钱包余额'
    )
    
    # 统计信息
    post_count = models.IntegerField(
        default=0,
        verbose_name='发帖数量'
    )
    comment_count = models.IntegerField(
        default=0,
        verbose_name='评论数量'
    )
    like_count = models.IntegerField(
        default=0,
        verbose_name='获赞数量'
    )
    
    class Meta:
        verbose_name = '用户详细信息'
        verbose_name_plural = '用户详细信息'
        db_table = 'user_profiles'
    
    def __str__(self):
        return f"{self.user.username}的详细信息"


class UserAddress(models.Model):
    """
    用户地址模型
    存储用户的收货地址信息
    """
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE,
        related_name='addresses',
        verbose_name='用户'
    )
    
    # 地址信息
    receiver_name = models.CharField(
        max_length=50,
        verbose_name='收货人姓名'
    )
    receiver_phone = models.CharField(
        max_length=11,
        verbose_name='收货人电话'
    )
    address = models.CharField(
        max_length=200,
        verbose_name='详细地址'
    )
    
    # 地址类型
    is_default = models.BooleanField(
        default=False,
        verbose_name='是否默认地址'
    )
    
    # 时间信息
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间'
    )
    
    class Meta:
        verbose_name = '用户地址'
        verbose_name_plural = '用户地址'
        db_table = 'user_addresses'
        ordering = ['-is_default', '-created_at']
    
    def __str__(self):
        return f"{self.user.username} - {self.receiver_name}"


class Blacklist(models.Model):
    """
    黑名单模型
    用于管理被禁用的用户
    """
    
    user = models.OneToOneField(
        User, 
        on_delete=models.CASCADE,
        verbose_name='用户'
    )
    
    # 黑名单信息
    reason = models.TextField(
        verbose_name='拉黑原因'
    )
    admin_user = models.ForeignKey(
        User, 
        on_delete=models.SET_NULL,
        null=True,
        related_name='blacklist_actions',
        verbose_name='操作管理员'
    )
    
    # 时间信息
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='拉黑时间'
    )
    expires_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='解封时间'
    )
    
    class Meta:
        verbose_name = '黑名单'
        verbose_name_plural = '黑名单'
        db_table = 'user_blacklist'
    
    def __str__(self):
        return f"{self.user.username} - {self.reason[:20]}"
